package com.pucong.gitee.database.backup.business.database.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pucong.gitee.database.backup.business.database.cover.DataSearchSqlBeanConvert;
import com.pucong.gitee.database.backup.business.database.mapper.AutoSearchSqlInfoMapper;
import com.pucong.gitee.database.backup.common.entity.database.BackupSearchSqlInfo;
import com.pucong.gitee.database.backup.common.entity.database.dto.BackupSearchSqlInfoSaveDto;
import com.pucong.gitee.database.backup.common.entity.sys.UserJwt;
import com.pucong.gitee.database.backup.utils.UUID;
import com.pucong.gitee.database.backup.utils.UserRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AutoSearchSqlInfoServiceImpl implements AutoSearchSqlInfoService {

    @Autowired
    private AutoSearchSqlInfoMapper autoSearchSqlInfoMapper;

    @Autowired
    private DataSearchSqlBeanConvert dataSearchSqlBeanConvert;

    @Override
    public List<BackupSearchSqlInfo> getSearchSqlInfoControllerList() {
        UserJwt loginUser = UserRequest.getLoginUser();
        LambdaQueryWrapper<BackupSearchSqlInfo> params = new LambdaQueryWrapper<>();
        params.eq(BackupSearchSqlInfo::getCreateBy, loginUser.getUuid());
        List<BackupSearchSqlInfo> backupSearchSqlInfoList = autoSearchSqlInfoMapper.selectList(params);
        return backupSearchSqlInfoList;
    }

    @Override
    public BackupSearchSqlInfo getSearchSqlInfoById(String id) {
        return autoSearchSqlInfoMapper.selectById(id);
    }

    @Override
    public int saveInfo(BackupSearchSqlInfoSaveDto dto) {
        BackupSearchSqlInfo backupSearchSqlInfo = dataSearchSqlBeanConvert.dto2do(dto);
        if(StringUtils.isBlank(dto.getUuid())){
            UserJwt loginUser = UserRequest.getLoginUser();
            backupSearchSqlInfo.setUuid(UUID.uuid());
            backupSearchSqlInfo.setCreateBy(UUID.uuid());
            backupSearchSqlInfo.setExeTime(new Date());
            autoSearchSqlInfoMapper.insert(backupSearchSqlInfo);
        }else{
            autoSearchSqlInfoMapper.updateById(backupSearchSqlInfo);
        }
        return 1;
    }
}
